// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    'button-class': {width: '20px'},
    products: [
      {
        id:'a',name:'手机华为',price:10,num:1
      },{
        id:'b',name:'手机小米',price:20,num:2
      },{
        id:'c',name:'手机三星',price:30,num:3
      },{
        id:'d',name:'手机苹果',price:40,num:4
      },{
        id:'e',name:'手机酷派',price:45,num:4
      }
    ],
    list: ['a', 'b', 'c', 'd', 'e'],
    result: [],
    allChecked: false,
    totalprice: 0,
    show: true
  },
  onallCheckedChange(e) {
    //console.log(e)
    this.setData({
      allChecked: e.detail,
    })

    this.checkAll()
  },
  
  checkAll() {
    console.log(this.data.allChecked)
    let allcheck = this.data.allChecked

    if(allcheck) {
      let ids = new Set()
      this.data.list.forEach(v=>{
        ids.add(v)
      })

      console.log(ids)

      this.setData({
        result: Array.from(ids)
      })
    } else {
      let ids = []
      this.setData({
        result: ids
      })
    }

    //监听总价
    this.calculationPrice()
  },

  onChange(event) {
    console.log(event.detail)
    this.setData({
      result: event.detail
    });

    console.log('result:' + this.data.result.length)
    console.log('products' + this.data.products.length)
    //全选显示
    if(this.data.result.length == this.data.products.length){
      this.setData({
        allChecked: true
      })
    }
    //全选不显示
    if(this.data.result.length < this.data.products.length){
      this.setData({
        allChecked: false
      })
    }

    //监听总价
    this.calculationPrice()
  },

  onNumChange(value) {
    console.log('步进器id: ' + JSON.stringify(value))
    //console.log(JSON.stringify(detail))
    console.log(value.currentTarget.id)
    console.log(value.detail)

    let targetId = value.currentTarget.id
    let targetNum = value.detail

    let pros = this.data.products
    // pros.forEach(v=> {
    //   if(v.id == targetId){
    //     console.log('进入')
    //     let t = v.num
    //     this.setData({
    //       [t]: value.detail
    //     })
    //   }
    // })

    let _self = this
    //let index=0
    for(let i=0; i<pros.length; i++){
      
      if(targetId == pros[i].id) {
        console.log(pros[i])
        //index = i
        let up = 'products[' + i + '].num';//先用一个变量，把(info[0].gMoney)用字符串拼接起来
        _self.setData({
         [up]:targetNum
       })
      }
    }

      // let up = 'products[' + index + '].num';//先用一个变量，把(info[0].gMoney)用字符串拼接起来
      // console.log(up) 
      // _self.setData({
      //    [up]:targetNum
      // })

    
    console.log('   ')
    console.log(JSON.stringify(this.data.products))

    // const checkbox = this.selectComponent(`.checkboxes-${index}`);
    // console.log(checkbox)

    //监听总价
    this.calculationPrice()
  },

  /**
   * 监听总价
   */
  calculationPrice(){
    let total = 0
    let ps = this.data.products

    this.data.result.forEach(r => {
      ps.forEach(v => {
        if(r == v.id){
          total = total + v.price*v.num
        }
      })
    })

    console.log('总价: ' + total)

    this.setData({
      totalprice: 100*total  //转化为 vant 能识别的分为单位
    })
  },

  edit() {
    console.log('编辑')
    this.setData({
      show: false
    })
  },
 
  complete() {
    console.log('完成')
    this.setData({
      show: true
    })
  },

  noop() {

  },

  delete() {
    let ps = this.data.products
    console.log(ps);

    // this.data.result.forEach(r => {
    //   ps.forEach(v => {
    //     if(r == v.id){
    //       ps.remove(v)
    //     }
    //   })
    // })

    let ids = this.data.result
    console.log('初始' + JSON.stringify(ids))
    for(let i=0; i<ids.length; i++) {
      for(let j=0; j<ps.length; j++) {
        if(ids[i] == ps[j].id) {
          console.log(ps[j])
          console.log('匹配 ' + i)
          //ids.splice(i,1)
          ps.splice(j,1)
          break;
          
        }
      }
    }

    console.log('ids ' + JSON.stringify(ids))
    ids = []

    this.setData({
      products: ps
    })

    this.setData({
      result: ids
    })

    this.setData({totalprice:0})
    //this.setData({nums:})

    console.log(ps)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(this.products.length)
    

    // wx.setTabBarBadge({
    //   index: 2,
    //   text: '1'
    // })
  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})